package com.zmn.plat.admin.controller.product;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import com.zmn.common.dto.KvDTO;
import com.zmn.common.dto2.ResponseDTO;
import com.zmn.common.utils.string.StringUtil;
import com.zmn.plat.common.constant.ProductConsts;
import com.zmn.plat.common.exception.PlatException;
import com.zmn.plat.model.vo.product.ServProductDescVO;
import com.zmn.plat.model.vo.product.ServProductVO;

/**
 * 描述: 产品管理修改控制
 *
 * @author tanghongjun
 * @since: 2018/11/5 22:06
 */
@Controller
@RequestMapping("/product/")
public class ServProductModifyController extends ServProductBaseController {

    private Logger logger = LoggerFactory.getLogger(ServProductModifyController.class);

    /**
     * region 新增/修改
     *
     * @param servProductVO
     *            产品新增修改实体
     * @return ModelAndView 执行结果
     */
    @RequestMapping(value = "addEdit")
    @ResponseBody
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ServProductVO servProductVO) {
        String lable = "新增产品";
        servProductVO.setShowType(ProductConsts.ERP_PRODUCT_TYPE);

        try {
            String userName = getMcStaffRealName();
            if (servProductVO.getProductId() == null) {
                lable = "新增产品";
                String creater = getMcStaff().getRealName();
                Integer flag = servProductBService.insertAll(servProductVO, creater);
                return transAddFlagToMessage(flag);
            } else {
                Integer flag = servProductBService.updateAll(servProductVO, userName);
                return transModifyFlagToMessage(flag);
            }
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        } catch (Exception ex) {
            logger.error(String.format("[zmn]新增/修改产品失败,servProductVO:[%s],ex:[%s]", ex.getMessage(), ex));
            return ResponseDTO.fail(lable + "失败！");
        }
    }

    /**
     * 添加产品描述
     *
     * @param request
     * @param servProductDescVO
     * @return
     * @modifier tanbiao 20190930 代码抽离到父类
     */
    @RequestMapping(value = "addEditDesc")
    @ResponseBody
    public ResponseDTO addEdit(HttpServletRequest request, @RequestBody ServProductDescVO servProductDescVO) {
        return super.addEdit(getMcStaffRealName(), servProductDescVO);
    }

    /**
     * 描述: 批量设置产品排放量
     *
     * @param kvDTO
     *            产品ID串 排放量的值
     * @return ModelAndView 批量设置产品排放量页面
     */
    @RequestMapping("updateTotalEmissions")
    @ResponseBody
    public ResponseDTO updateTotalEmissions(HttpServletRequest request, @RequestBody KvDTO kvDTO) {
        if (StringUtil.isBlank(kvDTO.getKey().toString())) {
            return ResponseDTO.fail("至少选择一个产品");
        }

        List<Integer> productIds = StringUtil.convertStrsToList(kvDTO.getKey().toString());

        servProductService.updateTotalEmissionsByKeys(productIds, Integer.parseInt(kvDTO.getValue().toString()), getMcStaffRealName());

        return ResponseDTO.success(true, "设置成功");
    }

    /**
     * 描述:修改产品状态
     *
     * @param ids
     *            产品ID组成的字符串 eg: productId1,productId2
     * @param status
     *            修改的状态
     * @return: ResponseDTO 执行结果
     * @auther: tanghongjun
     * @since: 2018/11/5 22:39
     */
    @SuppressWarnings("Duplicates")
    @RequestMapping(value = "updateStatus")
    @ResponseBody
    public ResponseDTO updateStatus(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "ids", defaultValue = "") String ids, @RequestParam(value = "status", defaultValue = "0") int status) {

        if (StringUtil.isBlank(ids)) {
            return ResponseDTO.fail("请选择要更改状态的产品！");
        }

        List<Integer> productIds = StringUtil.convertStrsToList(ids);

        try {
            servProductBService.updateStatusBatch(productIds, status, getMcStaffRealName(), ProductConsts.ERP_PRODUCT_TYPE);
        } catch (PlatException e) {
            return ResponseDTO.fail(e.getMessage());
        }

        return ResponseDTO.success(true,"操作成功！");
    }

    /**
     * 功能描述 :修改产品排序
     *
     * @param request
     * @param response
     * @param productId
     * @param sort
     * @return com.zmn.common.dto2.ResponseDTO
     * @author tanbiao
     * @modifier
     * @since 2019/10/15
     */
    @RequestMapping(value = "updateSort")
    @ResponseBody
    public ResponseDTO updateSort(HttpServletRequest request, HttpServletResponse response,
        @RequestParam(value = "id", defaultValue = "0") Integer productId, @RequestParam(value = "sort", defaultValue = "0") Integer sort) {

        if (productId == 0) {
            return ResponseDTO.fail("请选择要更改排序的产品！");
        }
        if (sort == 0) {
            return ResponseDTO.fail("请设置产品排序值！");
        }

        servProductService.updateSortByKey(productId, sort, getMcStaffRealName());
        return ResponseDTO.success(true,"操作成功！");
    }

}
